package com.jdy.hotel.ui.login;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import androidx.lifecycle.ViewModelProviders;

import com.jdy.hotel.R;
import com.jdy.hotel.check.CheckHelper;
import com.jdy.hotel.net.ResponseData;
import com.jdy.hotel.support.BaseActivity;
import com.jdy.hotel.support.BaseViewModelFactory;
import com.jdy.hotel.ui.MainActivity;
import com.jdy.hotel.ui.forget.ForgetActivity;
import com.jdy.hotel.ui.register.RegisterActivity;

public class LoginActivity extends BaseActivity {
    /**
     * 保存账号密码
     */
    public static final String APP = "hotel";//文件名称
    public final static String KEEP_PASS = APP + "-" + "keep-pass";//是否保存密码
    public final static String ACCOUNT = APP + "-" + "account";//账号
    public final static String PASS = APP + "-" + "password";//密码

    /**
     * 账号和密码输入框
     */
    private EditText usernameEditText, passwordEditText;
    private CheckBox remember;//是否保存密码输入框

    /**
     * 绑定布局文件
     *
     * @return 布局文件
     */
    @Override
    protected int getLayoutSource() {
        return R.layout.activity_login;
    }

    /**
     * 初始化View
     *
     * @param savedInstanceState 携带的数据
     */
    @Override
    protected void createView(Bundle savedInstanceState) {
        usernameEditText = findViewById(R.id.login_user_name);
        passwordEditText = findViewById(R.id.login_user_password);
        remember = findViewById(R.id.login_action_remember);
        loadingProgressBar = findViewById(R.id.login_load_progress);
    }


    @Override
    protected void onRespond(ResponseData response) {
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        setResult(Activity.RESULT_OK);
        finish();
    }

    @Override
    protected void onFailure(ResponseData response) {
        if (response.getData() == null) { //出现异常，证明是系统问题
//            showMessage(response.getMessage());
        }
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.login_action_register).setOnClickListener(v -> startActivity(new Intent(LoginActivity.this, RegisterActivity.class)));

        findViewById(R.id.login_action_forget).setOnClickListener(v -> startActivity(new Intent(LoginActivity.this, ForgetActivity.class)));
    }

    @Override
    protected void bindData(Bundle savedInstanceState) {
        SharedPreferences sharedPreferences = getSharedPreferences(APP, Context.MODE_PRIVATE);
        boolean isKeep = sharedPreferences.getBoolean(KEEP_PASS, false);
        remember.setChecked(isKeep);
        if (isKeep) {
            usernameEditText.setText(sharedPreferences.getString(ACCOUNT, ""));
            passwordEditText.setText(sharedPreferences.getString(PASS, ""));
        }
    }

    @Override
    protected void bindModel() {
        LoginViewModel loginViewModel = ViewModelProviders.of(this, new BaseViewModelFactory()).get(LoginViewModel.class);

        CheckHelper.of(loginViewModel).editTake("userName", getString(R.string.prompt_email), usernameEditText)
                .editTake("Password", getString(R.string.prompt_password), passwordEditText).notNull();

        setFormObserver(loginViewModel.getFormData());
        setRequestObserver(loginViewModel.getRespondData());

        findViewById(R.id.login_action_sign_in).setOnClickListener(v -> {
            loadingProgressBar.setVisibility(View.VISIBLE);
            loginViewModel.login();
        });
    }

    @Override
    protected void onDestroy() {
        //保存状态
        @SuppressLint("CommitPrefEdits")
        SharedPreferences.Editor editor = getSharedPreferences(APP, Context.MODE_PRIVATE).edit();
        editor.putBoolean(KEEP_PASS, remember.isChecked());
        //是否记住密码
        if (remember.isChecked()) {
            //记住用户名和密码
            editor.putString(ACCOUNT, usernameEditText.getText().toString().trim());
            editor.putString(PASS, passwordEditText.getText().toString().trim());
        } else {
            editor.remove(ACCOUNT);
            editor.remove(PASS);
        }
        editor.apply();
        super.onDestroy();
    }
}
